package cn.edu.nuc.onlinestore.frame;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import UtilSever.Util;
import cn.edu.nuc.onlinestore.model.Carts;
import cn.edu.nuc.onlinestore.model.Client;
import cn.edu.nuc.onlinestore.model.Goods;
import cn.edu.nuc.onlinestore.model.UserCart;
import cn.edu.nuc.onlinestore.vo.Result;

public class UserStore extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JFrame UserLogin;
	private JFrame frame;
	private String str;




	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserStore frame = new UserStore();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}*/

	
	
	public JFrame getUserLogin() {
		return UserLogin;
	}



	public void setUserLogin(JFrame userLogin) {
		UserLogin = userLogin;
	}



	/**
	 * Create the frame.
	 */
	public UserStore() {
		this.frame=this;
		setTitle("中北在线商场--当前用户:"+UserCart.username);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 732, 467);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 78, 696, 341);
		panel.setLayout(new GridLayout(1, 1, 0, 0));
		
		DefaultTableModel model = new DefaultTableModel();
		model.addColumn("商品编号");
		model.addColumn("名称");
		model.addColumn("单价(人民币)");
		model.addColumn("库存");
		//model.addColumn("操作");
		
		
		
		
		
		/*model.addRow(new String[]{"1","水杯","15.00","200"});
		model.addRow(new String[]{"2","水瓶","35.00","200"});
		
		model.addRow(new String[]{"3","天堂伞","55.00","200"});
		model.addRow(new String[]{"4","男袜","8.00","200"});
		model.addRow(new String[]{"5","农夫山泉","2.00","200"});
		model.addRow(new String[]{"6","毛巾","9.90","200"});
		model.addRow(new String[]{"7","牙刷","15.00","200"});
		model.addRow(new String[]{"8","洗发水","15.00","200"});
		model.addRow(new String[]{"9","牙膏","15.00","200"});
		model.addRow(new String[]{"10","海尔全自动洗衣机","2,699.00","200"});*/
		
		
		JTable table = new JTable( model );
		/*//双击事件
		table.addMouseListener(new  MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				
					if(e.getClickCount()==2)
					{
						//得到当前选中商品项
						int row=table.getSelectedRow();
						//得到good id
						int id=Integer.parseInt(model.getValueAt(row, 0).toString());
						UserGoods d = new UserGoods(id);
						d.setUname(uname);
						d.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
						d.setVisible(true);
						}
					}	
		});*/
		JScrollPane pane = new JScrollPane( table );
		panel.add(pane);
		contentPane.add(panel);
		
		//显示所有商品，模糊查询
		showGoods(str,table,model);
		
		
		JButton button_2 = new JButton("查看商品详细信息");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
				//得到当前选中商品项
				int row=table.getSelectedRow();
				//判断
				if(row==-1)
				{
					JOptionPane.showConfirmDialog(null, "没有选择一行", "",JOptionPane.YES_NO_OPTION);
				}else{
					//得到good id
					int id=Integer.parseInt(model.getValueAt(row, 0).toString());
					//进入商品的详细信息列表
					UserGoods d = new UserGoods(id);
					d.setUserstore(frame);
					d.setUserlogin(UserLogin);
					d.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
					d.setVisible(true);
				}
		
				
			}
		});
		button_2.setBounds(407, 45, 299, 23);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("退出登录");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//关闭本页面
				Result<Client> r=Util.SeverSelectAClient(UserCart.username);
				Util.deletheClient((Client)r.getObj());
				frame.dispose();
				UserLogin.setVisible(true);
				
			}
		});
		button_3.setBounds(613, 6, 93, 23);
		contentPane.add(button_3);
		
		JLabel lblid = new JLabel("商品名称:");
		lblid.setBounds(10, 53, 65, 15);
		contentPane.add(lblid);
		
		textField = new JTextField();
		textField.setBounds(85, 46, 104, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		
		
		JButton button_4 = new JButton("搜索(模糊查询，用名字或id或单字)");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				str=textField.getText();
				showGoods(str,table,model);
			}
		});
		
		
		
		button_4.setBounds(197, 45, 93, 23);
		contentPane.add(button_4);
		
		
		
		
	
		
		
		//从服务器获取购物车
		Carts car=Util.SeverGetCarts(UserCart.username);
		int totalnum=0;
		//判断购物车是否为空
		if(car!=null)
		{
			Map<Goods,Integer> goodlist=car.getBuygood();
			Set<Entry<Goods, Integer>> l=goodlist.entrySet();
			Iterator<Entry<Goods, Integer>> en=l.iterator();
			while(en.hasNext())
			{
				Map.Entry<Goods, Integer>  g=en.next();
				
				Goods good=g.getKey();
				int number=g.getValue();
				totalnum+=number;
			}
		}
		UserGoodNumber.tonumber=totalnum;
		//显示购物车的商品数量
		JLabel label = new JLabel(/*"购物车: "+UserGoodNumber.tonumber*/);
		UserGoodNumber.label=label;
		UserGoodNumber.label.setText("购物车："+UserGoodNumber.tonumber);
		label.setBounds(10, 10, 124, 15);
		contentPane.add(label);
		
		
		
		
		JButton button_5 = new JButton("查看购物车");
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				//从服务器获取购物车
				Carts car=Util.SeverGetCarts(UserCart.username);
				//System.out.println("gggggggggggggggggggggggg"+car);
				//判断购物车是否为空
				if(car==null)
				{
					JOptionPane.showConfirmDialog(null, "购物车为空", "",JOptionPane.YES_NO_OPTION);
				}else{
					//进入购物车页面
					UserCartFrame cf = new UserCartFrame(car);
					
					cf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
					cf.setVisible(true);
				}
				
			}
		});
		button_5.setBounds(116, 6, 110, 23);
		contentPane.add(button_5);
	}
	
	
	//实现页面开始的查询功能
	public Set<Goods> showGoods(String str,JTable table,DefaultTableModel model)
	{
		Set<Goods> set = null;
		Goods g=null;
		//先删除所有行
		for (int i =model.getRowCount()-1; i >=0 ;i--) 
		{
			   model.removeRow(i);
		}
		//如果字符串为空
		if("".equals(str)||null==str)
		{
			//连接服务器查找
			set=Util.SeverSelectAllGoods();
			Iterator<Goods> goods = set.iterator();
			while(goods.hasNext())
			{
				g=goods.next();
				model.addRow(new String[]{""+g.getGoodsId(),g.getName(),""+g.getPeice(),""+g.getNumber()});
			}
		}else{
			
			//查找再添加
			try{
				g=Util.SeverSelectAGood(Integer.parseInt(str));
				
				if(g!=null)
				{
					model.addRow(new String[]{""+g.getGoodsId(),g.getName(),""+g.getPeice(),""+g.getNumber()});
					
				}	

			}catch(Exception e){
				
				//这里根据名字查询
				Set<Goods> d=Util.SeverSelecttheGood(str);
				Iterator<Goods> g1=d.iterator();
				while(g1.hasNext())
				{
					Goods g2= g1.next();
					model.addRow(new String[]{""+g2.getGoodsId(),g2.getName(),""+g2.getPeice(),""+g2.getNumber()});
					
				}
			}
			
			table.setModel(model);
			
		}
		return set;
	}
}
